define(['app'], function(app) {
    'use strict';

    var controller = function controller($scope, Secondkill, GoPage, $ionicSlideBoxDelegate,$timeout) {
        $scope.$on('$ionicView.afterEnter', function() {
            //动态设置高度
            GoPage.init_top($scope, 78, 0);
            $scope.init_data();
        }, false);
        //导航--切换
        $scope.selectIndex = 0;
        $scope.slideChanged = function(index) {
            $scope.selectIndex = index;
        };
        $scope.activeSlide = function(index) {
            $scope.selectIndex = index;
            $ionicSlideBoxDelegate.$getByHandle('main-handle').slide(index);
        };

        //页面跳转--发布活动
        //编辑
        $scope.onshow_groupseckill = function(act_id) {
                GoPage.go_states("groupseckill", {
                    "act_id": act_id
                });
            }
            //统计
        $scope.onshow_statistics_second = function(act_id) {
                GoPage.go_states("statistics_second", {
                    "id": act_id
                });
            }
            //抽奖
        $scope.onshow_connectagency = function(act_id) {
                GoPage.go_states("connectagency", {
                    "id": act_id
                });
            }
            //抽奖结果
        $scope.onshow_luckydrawresult = function(act_id) {
            GoPage.go_states("luckydrawresult", {
                "act_id": act_id
            });
        }

        $scope.init_data = function() {
            Secondkill.secondkill_ing(1,1, $scope.selectIndex + 1, function(data) {
                $scope.data = data;

            });

        }

        //下拉刷新
        $scope.input = new Object();
        $scope.input.nPage = 1;
        $scope.isLoad = false; //可以加载
        //下拉刷新 判断哪个模块进行刷新
        $scope.doRefresh = function() {
            //防止重复加载
            $scope.isLoad = true;
            Secondkill.secondkill_ing(1,1, $scope.selectIndex + 1, function(data) {
                    console.log(data);
                    if(data != null) {
                        $scope.data = data;

                    } else
                        $scope.data = new Object();
                    $scope.input.nPage = 1;
                },
                function() {
                    $scope.$broadcast('scroll.refreshComplete');
                    $timeout(function() {
                        $scope.isLoad = false;
                    }, 1500);
                });
        };
        //加载更多
        $scope.doRefreshMore = function() {
            //防止重复加载
            $scope.isLoad = true;
            Secondkill.secondkill_ing(1,$scope.input.nPage +1, $scope.selectIndex + 1, function(data) {
                    console.log(data);
                    if(data != null) {
                        if($scope.data.act != undefined) {
                            $scope.data.act = $scope.data.act.concat(data.act);
                            $scope.input.nPage += 1;
                        } else {
                            $scope.data = data;
                        }

                    }
                },
                function() {
                    $scope.$broadcast('scroll.infiniteScrollComplete');
                    $timeout(function() {

                        $scope.isLoad = false;
                    }, 1500);

                });

        };

    }

    controller.$inject = ['$scope', 'Secondkill', 'GoPage', '$ionicSlideBoxDelegate','$timeout'];

    return app.registerController("secondkillctrl",controller);
});